package com.water.bs.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;
//工具类加上 @Component注解
@Component
public class MailUtil {
    @Autowired
    private JavaMailSenderImpl mailSender;
    public  void sendMail(String code,String userEmail){//注册验证邮箱
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setSubject("验证你的邮箱");
        simpleMailMessage.setText("您的验证码是:"+code+",请在5分钟内完成验证。(本条消息为自动发送，请勿回复!)");
        simpleMailMessage.setTo(userEmail);
        simpleMailMessage.setFrom("694886886@qq.com");
        mailSender.send(simpleMailMessage);
    }
    public void sendOrgApplyEmail(String msg,String orgEmail,Boolean isAgree){//机构申请审批后的提示邮件
        System.out.println("机构审批通知邮件:"+msg+orgEmail+isAgree);
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setSubject("爱心机构审批通知");
        if (isAgree){//审批通过
            simpleMailMessage.setText("您的申请已经通过,请尽快登入完善信息,并发布物资需求!");
        }else{
            simpleMailMessage.setText("抱歉,您的申请未通过,原因:"+msg);
        }
        simpleMailMessage.setTo(orgEmail);
        simpleMailMessage.setFrom("694886886@qq.com");
        mailSender.send(simpleMailMessage);
    }
    //验证码是否在有效期内
    public Boolean emailCodeAble(String sendTime){
        //注册请求的时间 减去验证码发送时间来判断验证码是否再有效期内 5分钟
        return  (System.currentTimeMillis() - Long.valueOf(sendTime)) <5*60*1000;

    }
}
